//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.CompositeUI.Services;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.ObjectBuilder;

using LitwareHR.PublicApp.WinUX.Constants;
using LitwareHR.PublicApp.WinUX.Properties;
using LitwareHR.PublicApp.WinUX.Interface.Services;
using LitwareHR.Infrastructure.Interface.Constants;

using System.Xml;
using System.IO;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Reflection;


namespace LitwareHR.PublicApp.WinUX.Services
{
    public class RolesIconService
    {
        private Dictionary<string, BitmapImage> rolesIconsList;
        
        private BitmapImage defaultRoleIcon;
        private string defaultIconURL;

        const string NOIMAGEPATH = @"\Images\RolesIcons\No-image.png";

        [InjectionConstructor]
        public RolesIconService()
        {
            defaultIconURL = String.Format(@"{0}\{1}", System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), NOIMAGEPATH);
            Intialize();
        }

        /// <summary>
        /// Returns the image of the role
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public BitmapImage GetImage(string roleName)
        {
            if (!rolesIconsList.ContainsKey(roleName))
            {
                return defaultRoleIcon;
            }

            return rolesIconsList[ roleName ];
        }

        /// <summary>
        /// Reads Roles Config File to extract image location.
        /// </summary>
        private void Intialize()
        {
            rolesIconsList = new Dictionary<string, BitmapImage>();

            //Read Roles icon config file from Bin
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(File.ReadAllText("RolesIcon.xml"));

            foreach (XmlNode XN in doc.ChildNodes[0].ChildNodes)
            {
                string role = XN.Attributes["Title"].Value;

                if (!rolesIconsList.ContainsKey(role))
                {
                    string iconURL = GetRoleBitmapURL(XN.Attributes["Icon"].Value);
                    //rolesIconsURL.Add(role, iconURL);
                    rolesIconsList.Add(role, LoadIcon( iconURL ));
                }
            }

            defaultRoleIcon = LoadIcon( defaultIconURL );
        }

        private static BitmapImage LoadIcon( string iconURL )
        {
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.UriSource = new Uri(iconURL, UriKind.Absolute);
            bitmapImage.EndInit();
            return bitmapImage;
        }

        private string GetRoleBitmapURL(string roleIcon)
        {
            string fileName = String.Format(CultureInfo.InvariantCulture
                                    ,@"{0}\{1}"
                                    , Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                                    , roleIcon);

            if( !File.Exists(fileName) )
            {
                return defaultIconURL;
            }

            return fileName;
        }
    }
}
